package com.neuedu.oasystem.service.impl;

import java.util.List;
import java.sql.Connection;
import java.sql.SQLException;

import com.neuedu.oasystem.dao.IEmployeeDao;
import com.neuedu.oasystem.dao.impl.EmployeeDaoImpl;
import com.neuedu.oasystem.entity.Employee;
import com.neuedu.oasystem.service.EmployeeService;
import com.neuedu.oasystem.util.DBHelper;

public class EmployeeServiceImpl implements EmployeeService {
	private IEmployeeDao employeeDao;

	public EmployeeServiceImpl() {
		super();
		this.employeeDao = new EmployeeDaoImpl();
	}

	@Override
	public boolean add(Employee param) {
		Connection conn = DBHelper.openConnection();
		try {
			conn.setAutoCommit(false);
			employeeDao.setConnection(conn);
			boolean ret = employeeDao.add(param);
			conn.commit();
			conn.setAutoCommit(true);
			return ret;
		} catch (SQLException e) {
			if (conn != null) {
				try {
					conn.rollback();
					conn.setAutoCommit(true);
				} catch (SQLException e1) {

				}

			}
			e.printStackTrace();
		} finally {
			DBHelper.closeAll(null, null, conn);
		}
		return false;
	}

	@Override
	public boolean edit(Employee param) {
		Connection conn = DBHelper.openConnection();
		try {
			conn.setAutoCommit(false);
			employeeDao.setConnection(conn);
			boolean ret = employeeDao.edit(param);
			conn.commit();
			conn.setAutoCommit(true);
			return ret;
		} catch (SQLException e) {
			if (conn != null) {
				try {
					conn.rollback();
					conn.setAutoCommit(true);
				} catch (Exception e2) {
				}
			}
			e.printStackTrace();
		} finally {
			DBHelper.closeAll(null, null, conn);
		}
		return false;
	}

	@Override
	public boolean deleteById(int id) {
		Connection conn = DBHelper.openConnection();
		try {
			conn.setAutoCommit(false);
			employeeDao.setConnection(conn);
			boolean ret = employeeDao.deleteById(id);
			conn.commit();
			conn.setAutoCommit(true);
			return ret;
		} catch (SQLException e) {
			if (conn != null) {
				try {
					conn.rollback();
					conn.setAutoCommit(true);
				} catch (Exception e2) {
				}
			}
			e.printStackTrace();
		} finally {
			DBHelper.closeAll(null, null, conn);
		}
		return false;
	}

	@Override
	public Employee findById(int empId) {
		Connection conn = DBHelper.openConnection();
		try {
			conn.setAutoCommit(false);
			employeeDao.setConnection(conn);
			Employee employee = employeeDao.findById(empId);
			conn.commit();
			conn.setAutoCommit(true);
			return employee;
		} catch (SQLException e) {
			if (conn != null) {
				try {
					conn.rollback();
					conn.setAutoCommit(true);
				} catch (SQLException e1) {

				}

			}
			e.printStackTrace();
		} finally {
			DBHelper.closeAll(null, null, conn);
		}
		return null;
	}

	@Override
	public List<Employee> findAll() {
		Connection conn = DBHelper.openConnection();
		try {
			employeeDao.setConnection(conn);
			List<Employee> list = employeeDao.findAll();
			return list;
		} catch (Exception e) {
		} finally {
			DBHelper.closeAll(null, null, conn);
		}

		return null;
	}

	@Override
	public List<Employee> findByExample(Employee param) {
		Connection conn = DBHelper.openConnection();
		try {
			employeeDao.setConnection(conn);
			List<Employee> list = employeeDao.findByExample(param);
			return list;
		} catch (Exception e) {
		} finally {
			DBHelper.closeAll(null, null, conn);
		}
		return null;
	}

	@Override
	public List<Employee> findByPager(int page, int size) {
		Connection conn = DBHelper.openConnection();
		try {
			employeeDao.setConnection(conn);
			List<Employee> list = employeeDao.findByPager(page, size);
			return list;
		} catch (Exception e) {
		} finally {
			DBHelper.closeAll(null, null, conn);
		}
		return null;
	}

	@Override
	public int deleteByIds(int[] ids) {
		Connection conn = DBHelper.openConnection();
		try {
			employeeDao.setConnection(conn);
			int id = employeeDao.deleteByIds(ids);
			return id;
		} catch (Exception e) {
		} finally {
			DBHelper.closeAll(null, null, conn);
		}
		return 0;
	}

	@Override
	public int addAll(List<Employee> useparamrs) {
		Connection conn = DBHelper.openConnection();
		try {
			employeeDao.setConnection(conn);
			int all = employeeDao.addAll(useparamrs);
			return all;
		} catch (Exception e) {
		} finally {
			DBHelper.closeAll(null, null, conn);
		}
		return 0;
	}

	@Override
	public int editAll(List<Employee> param) {
		Connection conn = DBHelper.openConnection();
		try {
			employeeDao.setConnection(conn);
			int all = employeeDao.editAll(param);
			return all;
		} catch (Exception e) {
		} finally {
			DBHelper.closeAll(null, null, conn);
		}
		return 0;
	}

}
